package com.zhangtai.modules.service.impl.salary;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.Neusoft.constVo.EmployeeInfoConst;
//import com.zhangtai.Neusoft.service.NeuEmployeeBaseInfoService;
import com.zhangtai.modules.dao.SalaryDistributionDao;
import com.zhangtai.modules.dto.EmployeeSalaryGroupDto;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.salary.SalaryDistributionEntity;
import com.zhangtai.modules.service.salary.SalaryDistributionService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
public class SalaryDistributionServiceImpl extends ServiceImpl<SalaryDistributionDao, SalaryDistributionEntity> implements SalaryDistributionService {

    @Autowired
    private SalaryDistributionDao salaryDistributionDao;
//    @Autowired
//    private NeuEmployeeBaseInfoService neuEmployeeBaseInfoService;

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<SalaryDistributionEntity> page = this.page(
                new Page<SalaryDistributionEntity>(pageNum,pageSize),
                new QueryWrapper<SalaryDistributionEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public List<Map<String,Object>> queryPageList(
            @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
            @RequestParam(value = "orgId", required = false) List<Long> orgId,
            @RequestParam(value = "unitIds", required = false) List<Long> unitIds,
            @RequestParam(value = "jobIds", required = false) List<Long> jobIds,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "isDistribute", defaultValue = "0") Integer isDistribute,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {


        Page<EmployeeSalaryGroupDto> page = new Page<EmployeeSalaryGroupDto>(pageNum,pageSize);
        List<Map<String,Object>> list = salaryDistributionDao.getSalaryDistributionInfoList(
                orgIds,unitIds,jobIds,
                StringUtils.isEmpty(name)?null:name,
                StringUtils.isEmpty(code)?null:code,
                isDistribute,
                page);
        return list;
    }

    @Override
    public SalaryDistributionEntity saveSalaryDistribution(
            EmployeeInfoEntity employeeInfoEntity, Integer type) {
        SalaryDistributionEntity salaryDistributionEntity=salaryDistributionDao.selectOne(new QueryWrapper<SalaryDistributionEntity>()
                .eq("oid",employeeInfoEntity.getOid()).eq("is_delete", 0));
                if(ObjectUtils.isEmpty(salaryDistributionEntity)){
                    salaryDistributionEntity=new SalaryDistributionEntity();
                    salaryDistributionEntity.setOid(employeeInfoEntity.getOid());
                    salaryDistributionEntity.setSalaryGroupId(employeeInfoEntity.getSalaryGroupId());
                    salaryDistributionEntity.setPayrollUnitId(employeeInfoEntity.getPayrollUnitId());
//                    salaryDistributionEntity.setOrgId(employeeInfoEntity.getOrgId());
                    salaryDistributionEntity.setUnitId(employeeInfoEntity.getUnitId());
                    salaryDistributionEntity.setIsDistribute(0);
                    salaryDistributionEntity.setIsDelete(0);
                    salaryDistributionEntity.setType(type);
                    salaryDistributionEntity.setCreateTime(new Date());
                    salaryDistributionDao.insert(salaryDistributionEntity);
//                    neuEmployeeBaseInfoService.sendMq(salaryDistributionEntity, EmployeeInfoConst.EMPLOYEE_GET_SALARY_Z,"salaryDistributionEntity",false);
                }else {
                    salaryDistributionEntity.setIsDelete(0);
                    salaryDistributionEntity.setType(type);
                    salaryDistributionEntity.setIsDistribute(0);
                    this.updateByIdE(salaryDistributionEntity);
                }

        return salaryDistributionEntity;
    }
    @Override
    public SalaryDistributionEntity getByIdE(Long id) {
            SalaryDistributionEntity one = this.getOne(new QueryWrapper<SalaryDistributionEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(SalaryDistributionEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(SalaryDistributionEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            SalaryDistributionEntity entity = new SalaryDistributionEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}